package demo30.servlet;

import com.alibaba.fastjson.JSON;
import demo30.bean.WtyUser;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author:wangtianyu(王天宇)
 * @Date：Created on 2020/3/27 0027 19:51
 * @Description: web 服务 ： 配置一个请求url
 * 请求的方式：浏览器的地址栏请求缺省就是GET方式，会调用doGet方法
 * 我们可以通过postman来模拟PUT GET POST DELETE请求
 * 如果是程序里面则通过 Axios 插件
 *
 * 响应数据：
 * 1、html页面
 * 2、string（文本）
 * 3、json格式（最常用）
 *
 */
@WebServlet("/")
public class WtyHelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("GET");
        //服务器作出相应 ：写数据给用户
        resp.setCharacterEncoding("UTF-8"); // 设定写入的时候的编码，如果不规定会读取系统编码
        PrintWriter writer = resp.getWriter();
        //比如需要返回的是一个网页
        String html = "<!DOCTYPE html>" +
                "<html>" +
                "<head>" +
                "<meta charset=\"UTF-8\">" +
                "<title>第一个Servlet</title>" +
                "</head>" +
                "<body>" +
                "<h1>hello servlet</h1>" +
                "</body>" +
                "</html>";
        //写回页面
        writer.println(html);
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("POST");

        resp.setCharacterEncoding("UTF-8");
        PrintWriter writer = resp.getWriter();
//        writer.println("POST REQUEST"); // string
//        writer.write("{\"value\":\"POST REQUEST\"}"); // json
        WtyUser wtyUser = new WtyUser();
        wtyUser.setName("张三");
        wtyUser.setAge(21);

        writer.println(JSON.toJSONString(wtyUser));

        writer.flush();
        writer.close();
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("PUT");

        resp.setCharacterEncoding("UTF-8");
        PrintWriter writer = resp.getWriter();
//        writer.println("PUT REQUEST"); // string
//        writer.write("{\"value\":\"PUT REQUEST\"}"); // json
        WtyUser wtyUser = new WtyUser();
        wtyUser.setName("张三");
        wtyUser.setAge(21);

        writer.println(JSON.toJSONString(wtyUser));

        writer.flush();
        writer.close();
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("DELETE");

        resp.setCharacterEncoding("UTF-8");
        PrintWriter writer = resp.getWriter();
//        writer.println("DELETE REQUEST"); // string
//        writer.write("{\"value\":\"DELETE REQUEST\"}"); // json
        WtyUser wtyUser = new WtyUser();
        wtyUser.setName("张三");
        wtyUser.setAge(21);

        writer.println(JSON.toJSONString(wtyUser));

        writer.flush();
        writer.close();
    }
}
